package cc.fine.svc.main.service.impl.rel;

import cc.fine.svc.main.bean.es.UserSearchEsEntity;
import cc.fine.svc.main.bean.po.RelUserFan;
import cc.fine.svc.main.bean.po.TblUser;
import cc.fine.svc.main.bean.po.TblUserCount;
import cc.fine.svc.main.mapper.RelUserFanMapper;
import cc.fine.svc.main.service.rel.RelUserFanService;
import cc.fine.svc.main.service.tbl.TblUserCountService;
import cc.fine.svc.main.service.tbl.TblUserService;
import cc.fine.svc.main.utils.EsUtil;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wyihe
 * @since 2023-10-29
 */
@Service
@Transactional
public class RelUserFanServiceImpl extends ServiceImpl<RelUserFanMapper, RelUserFan> implements RelUserFanService {

    @Autowired
    private TblUserCountService userCountService;

    @Autowired
    private EsUtil esUtil;

    @Autowired
    private RelUserFanService fanService;

    @Override
    public void follow(RelUserFan fan) {
        // 如果已经关注了就不要再关注了
        QueryWrapper<RelUserFan> w = new QueryWrapper<>();
        w.eq("master_id", fan.getMasterId());
        w.eq("follower_id", fan.getFollowerId());
        if (!fanService.list(w).isEmpty()) {
            return;
        }

        save(fan);

        // fllowerCount 的 self_attention + 1
        // masterCount 的 fans + 1
        String followerId = fan.getFollowerId();
        TblUserCount fllowerCount = userCountService.getById(followerId);
        if (fllowerCount == null) fllowerCount = new TblUserCount();
        fllowerCount.setSelfAttention(fllowerCount.getSelfAttention() + 1);

        TblUserCount masterCount = userCountService.getById(fan.getMasterId());
        if (masterCount == null) masterCount = new TblUserCount();
        masterCount.setFans(masterCount.getFans() + 1);
        masterCount.setId(fan.getMasterId());
        fllowerCount.setId(fan.getFollowerId());
        userCountService.saveOrUpdateBatch(Arrays.asList(fllowerCount, masterCount));


        // 数据写入 es
        UserSearchEsEntity entity = new UserSearchEsEntity();
        entity.setId(fan.getMasterId());
        BeanUtils.copyProperties(masterCount, entity);

        try {
            esUtil.updateDataById(entity, TblUserService.User_Search_Es_Index,fan.getMasterId());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<TblUser> fans(String userId, Integer page, Integer limit) {
        List<TblUser> res = baseMapper.selectFans(userId, page * limit, limit);
        if (res == null) res = new ArrayList<>();
        return res;
    }

    @Override
    public void disFollow(RelUserFan fan) {
        // 如果没有关注，就不要再取消了
        QueryWrapper<RelUserFan> w = new QueryWrapper<>();
        w.eq("master_id", fan.getMasterId());
        w.eq("follower_id", fan.getFollowerId());
        if (fanService.list(w).isEmpty()) {
            return;
        }

        remove(w);

        // fllowerCount 的 self_attention - 1
        // masterCount 的 fans - 1
        String followerId = fan.getFollowerId();
        TblUserCount fllowerCount = userCountService.getById(followerId);
        if (fllowerCount == null) fllowerCount = new TblUserCount();
        fllowerCount.setSelfAttention(fllowerCount.getSelfAttention() - 1);

        TblUserCount masterCount = userCountService.getById(fan.getMasterId());
        if (masterCount == null) masterCount = new TblUserCount();
        masterCount.setFans(masterCount.getFans() - 1);
        masterCount.setId(fan.getMasterId());
        fllowerCount.setId(fan.getFollowerId());
        userCountService.saveOrUpdateBatch(Arrays.asList(fllowerCount, masterCount));


        // 数据写入 es
        UserSearchEsEntity entity = new UserSearchEsEntity();
        entity.setId(fan.getMasterId());
        BeanUtils.copyProperties(masterCount, entity);

        try {
            esUtil.updateDataById(entity, TblUserService.User_Search_Es_Index,fan.getMasterId());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int isFan(String followerId, String masterId) {
        int res = 0;
        QueryWrapper<RelUserFan> w = new QueryWrapper<>();
        w.eq("follower_id", followerId);
        w.eq("master_id", masterId);
        if (fanService.getOne(w) != null) {
            res = 1;
        }

        QueryWrapper<RelUserFan> w2 = new QueryWrapper<>();
        w2.eq("follower_id", masterId);
        w2.eq("master_id", followerId);
        if (res == 1 && fanService.getOne(w2) != null) {
            res = 2;
        }

        return res;
    }

}
